<?php

namespace App\Http\Controllers\Admin;

use App\Model\UserWithdraw;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class WithdrawController extends BaseController
{
    //审核
    public function change(Request $request)
    {
        if(!$this->loginUser()){
            return $this->failed('请登录');
        }

        if($this->loginUser()->userType != "admin"){
            return $this->failed('没有权限');
        }
        
        $bool = $this->requestValidate($request->all(),[
            'ids' => 'required',
            'state' => 'required|in:pass,refuse',
            'remark'=>'required_if:state,refuse',
        ]);

        if (!$bool){
            return $this->failed($this->error);
        }

        $ids = explode(',',$request->input('ids'));

        $state = $request->input('state') == "pass"?1:0;

        if ($state == 1){
            $pids = [];

            foreach ($ids as $id){
                $uw = UserWithdraw::where('state','=',0)->find($id);
                if (!$uw) continue;
                $uw->state = 1;
                $uw->pass_time = date('Y-m-d H:i:s');
                if($uw->save()) {
                    $pids[] = $uw->id;
                    $uw->createHistory($this->loginUserId());
                }
            }

            if (count($pids)>0){
                DB::table('t_user_points')->whereIn('obj_id',$pids)->where('obj_type','=','withdraw')->update([
                    'state' => 'withdraw-pass',
                    'stateName' => '待打款'
                ]);
            }
            return $this->success(['total'=>count($pids)]);
        }

        if ($state == 0){
            $pids = [];
            $uid = [];

            foreach ($ids as $id){
                $uw = UserWithdraw::where('state','=',0)->find($id);
                if (!$uw) continue;
                $uw->state = 3;
                $uw->finished_time = date('Y-m-d H:i:s');
                if($uw->save()) {
                    $pids[] = $uw->id;
                    $uid[] = $uw->user_id;
                    $uw->createHistory($this->loginUserId());
                }
            }

            if (count($pids)>0){
                DB::table('t_user_points')->whereIn('obj_id',$pids)->where('obj_type','=','withdraw')->update([
                    'state' => 'withdraw-refuse',
                    'stateName' => '审核拒绝',
                    'ok'=>0
                ]);
            }

            if (count($uid)>0){
                $uid = array_unique($uid);
                foreach ($uid as $id){
                    User::updateUserPoint($id);
                }
            }

            return $this->success(['total'=>count($pids)]);
        }

        return $this->failed('fail');
    }

    //已打款
    public function complete(Request $request)
    {
        if(!$this->loginUser()){
            return $this->failed('请登录');
        }

        if($this->loginUser()->userType != "admin"){
            return $this->failed('没有权限');
        }

        $bool = $this->requestValidate($request->all(),[
            'ids' => 'required',
        ]);

        if (!$bool){
            return $this->failed($this->error);
        }

        $ids = explode(',',$request->input('ids'));
        $pids = [];
        $uid=[];

        foreach ($ids as $id){
            $uw = UserWithdraw::where('state','=',1)->find($id);
            if (!$uw) continue;
            $uw->state = 2;
            $uw->finished_time = date('Y-m-d H:i:s');
            $uid[] = $uw->user_id;

            if($uw->save()) {
                $pids[] = $uw->id;
                $uw->createHistory($this->loginUserId());
            }
        }

        if (count($pids)>0){
            DB::table('t_user_points')->whereIn('obj_id',$pids)->where('obj_type','=','withdraw')->update([
                'state' => 'withdraw-success',
                'stateName' => '已打款'
            ]);
        }

        if (count($uid)>0){
            $uid = array_unique($uid);
            foreach ($uid as $id){
                User::updateUserPoint($id);
            }
        }

        return $this->success(['total'=>count($pids)]);

    }

}
